/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cadProdutoJInternalFrame.java
 *
 * Created on 27/10/2009, 21:03:56
 */
package br.com.fatecpg.zanotti.jframes.cadastros;

import br.com.fatecpg.zanotti.acessobanco.conComboFornecedor;
import br.com.fatecpg.zanotti.acessobanco.conComboLoja;
import br.com.fatecpg.zanotti.acessobanco.conTableProduto;
import br.com.fatecpg.zanotti.acessobanco.dmlProduto;
import br.com.fatecpg.zanotti.jframes.consultas.conProdutoJInternalFrame;
import br.com.fatecpg.zanotti.modelo.Produto;

/**
 *
 * @author Luiz Zanotti
 */
public class cadProdutoJInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form cadProdutoJInternalFrame */
    public cadProdutoJInternalFrame() {
        initComponents();
        conComboFornecedor conComboForn = new conComboFornecedor();

        FornecedorjComboBox.removeAllItems();
        FornecedorjComboBox.setModel(conComboForn.consulta());

        conComboLoja con = new conComboLoja();

        String[] teste = con.consulta();

        lojajComboBox.removeAllItems();
        lojajComboBox.addItem(teste[0]);
        lojajComboBox.addItem(teste[1]);

        conTableProduto con1 = new conTableProduto();
        codigojTextField.setText(con1.ConsultaUltimoCDPRODUTO());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        codigojTextField = new javax.swing.JTextField();
        lojajComboBox = new javax.swing.JComboBox();
        FornecedorjComboBox = new javax.swing.JComboBox();
        nfjTextField = new javax.swing.JTextField();
        qtjTextField = new javax.swing.JTextField();
        modelojTextField = new javax.swing.JTextField();
        corjTextField = new javax.swing.JTextField();
        vlcomprajFormattedTextField = new javax.swing.JFormattedTextField();
        vlvendajFormattedTextField = new javax.swing.JFormattedTextField();
        icesgotadojCheckBox = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        cadastrarjButton = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Cadastro de Produtos");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setText("Código:");

        jLabel2.setText("Loja:");

        jLabel3.setText("Fornecedor:");

        jLabel4.setText("Nota Fiscal:");

        jLabel5.setText("Quantidade:");

        jLabel6.setText("Modelo:");

        jLabel7.setText("Cor:");

        jLabel8.setText("Valor Comp:");

        jLabel9.setText("Valor Vend:");

        codigojTextField.setEnabled(false);

        lojajComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        FornecedorjComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        icesgotadojCheckBox.setText("Esgotado");

        jButton1.setText("Selecionar Existente");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        cadastrarjButton.setText("Cadastrar");
        cadastrarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cadastrarjButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(icesgotadojCheckBox)
                    .addComponent(jButton1)
                    .addComponent(cadastrarjButton)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lojajComboBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, 164, Short.MAX_VALUE)
                            .addComponent(corjTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                            .addComponent(modelojTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                            .addComponent(FornecedorjComboBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, 164, Short.MAX_VALUE)
                            .addComponent(nfjTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                            .addComponent(qtjTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                            .addComponent(vlcomprajFormattedTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                            .addComponent(vlvendajFormattedTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                            .addComponent(codigojTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(codigojTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lojajComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(FornecedorjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(nfjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(qtjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(modelojTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(corjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(vlcomprajFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(vlvendajFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(icesgotadojCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cadastrarjButton)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cadastrarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadastrarjButtonActionPerformed
        conComboLoja con = new conComboLoja();
        conComboFornecedor conComboForn = new conComboFornecedor();

        Produto.getInstancia().setCDFORNECEDOR(conComboForn.consultaCDFORNECEDOR(FornecedorjComboBox.getSelectedItem().toString()));
        Produto.getInstancia().setCDLOJA(con.consultaCDLOJA(lojajComboBox.getSelectedItem().toString()));
        Produto.getInstancia().setCDNOTAFISCAL(nfjTextField.getText());
        Produto.getInstancia().setCDPRODUTO(codigojTextField.getText());
        Produto.getInstancia().setNMCOR(corjTextField.getText());
        Produto.getInstancia().setNMMODELO(modelojTextField.getText());
        Produto.getInstancia().setQTPRODUTO(qtjTextField.getText());
        Produto.getInstancia().setVLCOMPRA(vlcomprajFormattedTextField.getText());
        Produto.getInstancia().setVLVENDA(vlvendajFormattedTextField.getText());

        if (icesgotadojCheckBox.isSelected()) {
            Produto.getInstancia().setICESGOTADO("S");
        } else {
            Produto.getInstancia().setICESGOTADO("N");
        }
        boolean b = true;
        if (cadastrarjButton.getText().equals("Cadastrar")) {
            b = dmlProduto.cadastrar(Produto.getInstancia());
        } else {
            b = dmlProduto.atualizar(Produto.getInstancia());
        }

        if (b) {
            limpaTudo();
        }

        cadastrarjButton.setText("Cadastrar");
        this.setTitle("Cadastro de Produto");
    }//GEN-LAST:event_cadastrarjButtonActionPerformed
    public void limpaTudo() {
        vlcomprajFormattedTextField.setText("");
        vlvendajFormattedTextField.setText("");
        qtjTextField.setText("");
        modelojTextField.setText("");
        corjTextField.setText("");
        conTableProduto con1 = new conTableProduto();
        codigojTextField.setText(con1.ConsultaUltimoCDPRODUTO());
        nfjTextField.setText("");
        icesgotadojCheckBox.setSelected(false);
        lojajComboBox.setSelectedIndex(0);
        FornecedorjComboBox.setSelectedIndex(0);
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.setVisible(false);
        conProdutoJInternalFrame.getInstancia().setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed
    private static cadProdutoJInternalFrame p;

    public static cadProdutoJInternalFrame getInstancia() {
        if (p == null) {
            p = new cadProdutoJInternalFrame();
        }
        return p;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox FornecedorjComboBox;
    private javax.swing.JButton cadastrarjButton;
    private javax.swing.JTextField codigojTextField;
    private javax.swing.JTextField corjTextField;
    private javax.swing.JCheckBox icesgotadojCheckBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JComboBox lojajComboBox;
    private javax.swing.JTextField modelojTextField;
    private javax.swing.JTextField nfjTextField;
    private javax.swing.JTextField qtjTextField;
    private javax.swing.JFormattedTextField vlcomprajFormattedTextField;
    private javax.swing.JFormattedTextField vlvendajFormattedTextField;
    // End of variables declaration//GEN-END:variables

    /**
     * @param FornecedorjComboBox the FornecedorjComboBox to set
     */
    public void setcadButton(String s) {
        cadastrarjButton.setText(s);
    }

    public void setFornecedorjComboBox(String s) {
        conComboFornecedor con = new conComboFornecedor();
        this.FornecedorjComboBox.setSelectedItem(con.consultaNMFORNECEDOR(s));
    }

    /**
     * @param codigojTextField the codigojTextField to set
     */
    public void setCodigojTextField(String s) {
        this.codigojTextField.setText(s);
    }

    /**
     * @param corjTextField the corjTextField to set
     */
    public void setCorjTextField(String s) {
        this.corjTextField.setText(s);
    }

    /**
     * @param icesgotadojCheckBox the icesgotadojCheckBox to set
     */
    public void setIcesgotadojCheckBox(String s) {
        if (s.equals("S")) {
            this.icesgotadojCheckBox.setSelected(true);
        } else {
            this.icesgotadojCheckBox.setSelected(false);
        }
    }

    /**
     * @param lojajComboBox the lojajComboBox to set
     */
    public void setLojajComboBox(String s) {
        this.lojajComboBox.setSelectedIndex(Integer.parseInt(s) - 1);
    }

    /**
     * @param modelojTextField the modelojTextField to set
     */
    public void setModelojTextField(String s) {
        this.modelojTextField.setText(s);
    }

    /**
     * @param nfjTextField the nfjTextField to set
     */
    public void setNfjTextField(String s) {
        this.nfjTextField.setText(s);
    }

    /**
     * @param qtjTextField the qtjTextField to set
     */
    public void setQtjTextField(String s) {
        this.qtjTextField.setText(s);
    }

    /**
     * @param vlcomprajFormattedTextField the vlcomprajFormattedTextField to set
     */
    public void setVlcomprajFormattedTextField(String s) {
        this.vlcomprajFormattedTextField.setText(s);
    }

    /**
     * @param vlvendajFormattedTextField the vlvendajFormattedTextField to set
     */
    public void setVlvendajFormattedTextField(String s) {
        this.vlvendajFormattedTextField.setText(s);
    }
}
